package View;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JScrollPane;
import javax.swing.SpinnerNumberModel;

import Controller.DB;
import Controller.RUWChangeListener;
import Model.ClickableLabel;
import Model.Customer;
import Model.MenuItem;
import Model.Order;
import Model.RUWSpinner;

@SuppressWarnings("serial")
public class AccountInfoPanel extends JPanel {

	/**
	 * Create the panel.
	 */
	public AccountInfoPanel(final RestUWantGUI GUI, final Customer cust, final DB db) {
		setLayout(null);
				
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM);
		tabbedPane.setBounds(16, 16, GUI.getWidth() - 40, GUI.getHeight() - 150);
		
		JPanel infoPane = new JPanel();
		JPanel favoritePane = new JPanel();
		JPanel ordersPane = new JPanel();
		
		infoPane.setLayout(null);
		favoritePane.setLayout(new GridBagLayout());
		ordersPane.setLayout(new GridBagLayout());
		
		final JTextField name = new JTextField(cust.getName());
		name.setBounds(22, 40, 200, 25);
		infoPane.add(name);
		
		final JTextField email = new JTextField(cust.getUserID());
		email.setBounds(22, 70, 200, 25);
		email.setEnabled(false);
		infoPane.add(email);
		
		final JTextField street = new JTextField(cust.getStreet());
		street.setBounds(22, 100, 200, 25);
		infoPane.add(street);
		
		final JTextField city = new JTextField(cust.getCity());
		city.setBounds(22, 130, 100, 25);
		infoPane.add(city);
		
		final JTextField state = new JTextField(cust.getState());
		state.setBounds(22, 160, 30, 25);
		infoPane.add(state);
		
		final JTextField zip = new JTextField(cust.getZip());
		zip.setBounds(60, 160, 61, 25);
		infoPane.add(zip);
		
		final JTextField phone = new JTextField(cust.getPhone());
		phone.setBounds(22, 190, 100, 25);
		infoPane.add(phone);
		
		JButton btnUpdateInfo = new JButton("Update Info");
		btnUpdateInfo.setBounds(22, 230, 100, 30);
		btnUpdateInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cust.setName(name.getText());
				cust.setStreet(street.getText());
				cust.setCity(city.getText());
				cust.setState(state.getText());
				cust.setZip(zip.getText());
				cust.setPhone(phone.getText());
				db.updateCustomer(cust);
				JOptionPane.showMessageDialog(null, "Info Updated.");
			}
		});
		infoPane.add(btnUpdateInfo);
		
				
		
		List<Order> orderList = db.getOrdersByCustomer(cust);
		GridBagConstraints gc2 = new GridBagConstraints();
		gc2.gridx = 0;
		gc2.gridy = 0;
		for (final Order ord : orderList) {
			gc2.gridx = 0;
			gc2.ipadx = 10;
			ClickableLabel orderLabel = new ClickableLabel(Integer.toString(ord.getID()));
			orderLabel.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					 new OrderDetailPopup(GUI, cust, ord);
				}
			});
			ordersPane.add(orderLabel, gc2);
			gc2.gridx++;
			gc2.ipadx = 10;
			ordersPane.add(new JLabel(ord.getDate().toString()), gc2);
			gc2.gridx++;
//			JButton reOrder = new JButton("Re-Order");
//			reOrder.addActionListener(new ActionListener() {
//				@Override
//				public void actionPerformed(ActionEvent e) {
//					try {
//						int id = DB.getNextOrderID();
//						ord.setID(id);
//						cust.setOrder(ord);	
//						db.addOrder(cust);
//						GUI.orderToComplete();
//					} catch (SQLException e1) {
//						// TODO Auto-generated catch block
//						e1.printStackTrace();
//					}
//					
//				}
//			});
//			ordersPane.add(reOrder, gc2);
			gc2.gridy++;
		}
		
		GridBagConstraints gc = new GridBagConstraints();

		List<MenuItem> faveList = db.getFavorites(cust);
		
		gc.gridx = 0;
		gc.gridy = 0;
		gc.ipadx = 10;
		gc.ipady = 20;
		gc.anchor = GridBagConstraints.LINE_START;
		favoritePane.add(new JLabel("Choose items from your favorites list and click to order now!"), gc);
		gc.gridx = 1;
		gc.ipady = 20;
		gc.anchor = GridBagConstraints.LINE_END;
		JButton btnSubmit = new JButton("Submit Order");
		btnSubmit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					if(cust.getOrder().hasItems()) {
						db.addOrder(cust);
						GUI.orderToComplete();
					}	
				}
			});
		favoritePane.add(btnSubmit);
		
		gc.gridx = 0;
		gc.gridy = 1;
		gc.ipady = 0;
		for (MenuItem itm : faveList) {
			gc.gridx = 0;
			gc.anchor = GridBagConstraints.LINE_START;
			favoritePane.add(new ClickableLabel(itm.getName()), gc);
			gc.gridx++;
			gc.fill = GridBagConstraints.NONE;
			JSpinner spinner = new RUWSpinner(new SpinnerNumberModel(0, 0, 10, 1), itm, cust);
			spinner.addChangeListener(new RUWChangeListener());
			favoritePane.add(spinner, gc);
			gc.gridy++;
		}	
		
		JScrollPane faveScrollPane = new JScrollPane(favoritePane, 
				 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
				 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		faveScrollPane.setPreferredSize(new Dimension(GUI.getWidth() - 50, GUI.getHeight() - 150));
		
		JScrollPane orderScrollPane = new JScrollPane(ordersPane, 
				 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, 
				 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		orderScrollPane.setPreferredSize(new Dimension(GUI.getWidth() - 50, GUI.getHeight() - 150));
		
		tabbedPane.addTab("Acount Info", null, infoPane, null);
		tabbedPane.addTab("Past Orders", null, orderScrollPane, null);
		tabbedPane.addTab("Favorites", null, faveScrollPane, null);
		
		add(tabbedPane);

	}
}
